// 加载FORM表单扩展
layui.use(['form'], function(){
  var form = layui.form()
  ,layer = layui.layer;

	form.verify({
        code_name: function(value){
			if(value.length < 1) {
				return '请填写广告位名称';
			}
		  
		}
        ,apply_id: function(value){
            if(+value < 1) {
                return '请选择应用';
            }
        }
       ,code_type: function(value){
            if(+value < 1) {
                return '请选择广告位类型';
            }

        }
        ,width: function(value){
            if(+value < 1) {
                return '代码位宽度必须大于1';
            }
        }
        ,height: function(value){
            if(+value < 1) {
                return '代码位高度必须大于1';
            }
        }


	});

	//监听提交
	form.on('submit(submit)', function(data){

	  $.ajax({
		  url: "/Adsense/create",
		  type: "post",
		  data: data.field,
		  dataType: "json",
		  success: function (msg) {
			if(msg.status == 'success') {
				  PopModel.refreshParentFrame();
			} else {
				layer.msg(msg.msg, {icon: 5, shift: 6}, function(){});
			}
		  }
	  });
	});
	//监听select
	form.on('select(media)', function(data){
		var media_id =data.value;
		$.ajax({
			url: "/Code/MediaTogroup",
			type: "post",
			data: {'id':media_id} ,
			dataType: "json",
			success: function (msg) {
			  if(msg.status == 'success') {

				  $("#group_id").empty();
				  $("#group_id").append(msg.codegroup);
				  form.render(); 
			  } else {
				  console.log('有错误了!');
			  }
			}
		});
    });
  
	// 刷新表单
	form.render();
});